package com.huilian.iotbox.data.service.impl;

import com.alibaba.fastjson.JSON;
import com.huilian.iotbox.data.po.LogLogin;
import com.huilian.iotbox.data.service.LogLoginMQService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class LogLoginMQServiceImpl implements LogLoginMQService {

    @Resource
    private RabbitTemplate rabbitTemplate;


    @Override
    public void sendLogLoginToMQ(LogLogin logLogin) {
        this.sendLog(logLogin);
    }

    private void sendLog(LogLogin logLogin) {
        String json = JSON.toJSONString(logLogin);
        System.out.println("---------MQ发送前准备，将对象数据生成JSON数据--------" + json);
        log.info("--------发送操作日志MQ--------时间{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        CorrelationData correlationData = new CorrelationData();
        rabbitTemplate.convertAndSend("iotBox-logLogin", "iotBoxLogLogin", json, correlationData);
        log.info("--------用户登录失败发送MQ成功！--------时间{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}
